class Solution {
public:
    int rob(vector<int>& nums) {
        if(nums.size()==0){
            return 0;
        }
        if(nums.size()==1){
            return nums[0];
        }
        if(nums.size()==2){
            return nums[0]>nums[1]?nums[0]:nums[1];
        }
        if(nums.size()==3){
            //return nums[1]>nums[0]+nums[2]?nums[1]:nums[0]+nums[2];
            int max=nums[0];
            max=max>nums[1]?max:nums[1];
            max=max>nums[2]?max:nums[2];
            return max;
        }
        int left=max(nums,0,nums.size()-1);
        int right=max(nums,1,nums.size());
        return left>right?left:right;
    }

private:
    int max(vector<int>& nums,int l,int r){
        int max_i=0;
        int max_i_1=nums[l+1]>nums[l]?nums[l+1]:nums[l];
        int max_i_2=nums[l];
        for(int i=l+2;i<r;i++){
            max_i=max_i_1>max_i_2+nums[i]?max_i_1:max_i_2+nums[i];
            max_i_2=max_i_1;
            max_i_1=max_i;
        }
        return max_i;
    }
};